<link href="/coreassets/core/assets/jquery/css/redmond/jquery-ui-1.10.1.custom.css" rel="stylesheet">
<script src="/coreassets/core/assets/jquery/js/jquery-1.9.1.js"></script>
<script src="/coreassets/core/assets/jquery/js/jquery-ui-1.10.1.custom.js"></script>


<!--COLORPICKER -->

<script src="/coreassets/core/assets/colorpicker/jquery.colorpicker.js"></script>
<link href="/coreassets/core/assets/colorpicker/jquery.colorpicker.css" rel="stylesheet" type="text/css"/>
<script src="/coreassets/core/assets/colorpicker/i18n/jquery.ui.colorpicker-nl.js"></script>
<script src="/coreassets/core/assets/colorpicker/swatches/jquery.ui.colorpicker-pantone.js"></script>


<script type="text/javascript">
	$(document).ready(function(){
		$("#body").fadeIn("slow");
		$('[name=test_only]').colorpicker();
		
	});

	
	
	function loadpage(divId , url){
		
		$( '#'+divId ).load( url , function(){
			$( '#'+divId ).hide();
			$( '#'+divId ).fadeIn("slow");
		});
		return false;
	}

	function loadbyaction(actionURL , page , id){
		loadpagebyaction("div_" + page + "_" + id , actionURL + "/" + id , page , id);
	}

	

	function loadpagebyaction(divId , url , page , id){
		$( '#'+divId ).load( url , function(){
			var FORMID = "#form_" + page + "_" + id;
			var currentaction = "";
			
			if(url.indexOf("/edit/") != -1){
				url = url.replace("/edit/" , "/ajaxupdate/");
				currentaction = "update";
			}
			if(url.indexOf("/view/") != -1)
				currentaction = "view";
			
			if(url.indexOf("/add/") != -1){
				url = url.replace("/add/" , "/ajaxinsert/");
				currentaction = "insert";
			}
			
			$("#" + "tr_main_" + page +"_details_" + id ).fadeIn("slow");
		    $("#"+divId).fadeIn("slow");
			
			$(FORMID ).submit(function(event) {
				  event.preventDefault();
			    $.ajax({
				      url: url + "/"+page+"/" ,
				      type: "post",
				       data: $(FORMID).serialize(),
				      success: function(data){
					    //  alert(data.validationmessage);
					    //  alert(data.success);
					     if(data.success == "success"){
						      $(FORMID)[0].reset();
						     // alert(currentaction);
						     
						      $("#" + "tr_main_" + page +"_details_" + id ).fadeIn("slow");
						      $("#"+divId).fadeOut("slow");
						      
						      if(currentaction == "update"){
						    	 // loadpage(divId , url)
						    	  url = url.replace("/ajaxupdate/" , "/byfield_data_only/id/" + data.id);
								  loadpage("tr_main_" + page +"_details_" + data.id , url);
								  //alert(1111 + " ---- " + url)
						      }else if(currentaction == "insert"){
						    	  url = url.replace("/ajaxinsert/" , "/byfield_data_only/id/" + data.id);
						    	  //var colspan=$("#table_mainlist_"+page+" tr:first > td").length;
						    	  //alert(colspan);
						    	  $("#table_mainlist_"+page+" tr:first").after("<tr id = 'tr_main_" + page +"_details_" + data.id + "'></tr><tr id=tr_" + page +"_" + data.id +"><td colspan=5><div id='div_"+page+"_"+data.id+"'></div></td></tr>");
								//alert("LOADING");
							   	   loadpage("tr_main_" + page +"_details_" + data.id , url);
								  
						      }

					     }else{
					    	 $("#error_message_"+page+"_"+id).html(data.validationmessage);
					     }
				      },
				      error:function(data){
				          alert("failure");
				      }   
				    }); 
			  }); 
			
		});
		//alert("called");
		return false;
	}

	function addAnother(parentId, objIdToBeAdded){
		$("#" + parentId).append($("#" + objIdToBeAdded).html());
		var ids = $("#" + objIdToBeAdded +" :input");
		for(var i= 0 ; i < ids.length ; i++){
			//alert($(ids[i]).attr("name"));
		}
		return false;
	}


	function addAjaxSubmitListener(formId ,  resultDiv , destinationUrl , methodUponSuccess){
	
		$("#" + formId).submit(function(event) {
			  /* stop form from submitting normally */
			  event.preventDefault();
			//  alert(1111111111111111111);
			  /*clear result div*/
			   $("#" + resultDiv).html('');
	
			  /* get some values from elements on the page: */
	
			  /* Send the data using post and put the results in a div */
			    $.ajax({
			      url: destinationUrl,
			      type: "post",
			      data: $("#" + formId).serialize(),
			      success: function(data){
				    //  alert(methodUponSuccess);
			    	  var executeUponSuccess = methodUponSuccess;
			    	    if (executeUponSuccess != "") {
			    	        window[executeUponSuccess](formId , resultDiv , destinationUrl);
			    	    }
			      },
			      error:function(data){
			          alert("failure");
			       //   $("#" + resultDiv).html('there is error while submit');
			      }   
			    }); 
			});
	}

	function addAjaxClickSubmitListener(id , el , formId ,  resultDiv , destinationUrl , methodUponSuccess){
			$("#" + el).click(function(){
				 $.ajax({
				      url: destinationUrl,
				      type: "post",
				      //data: values,
				       data: $('#' + formId ).serialize(),
				      success: function(data){
				    	    if (methodUponSuccess != "") {
				    	    	$("#"+formId)[0].reset();
				    	        window[methodUponSuccess](id , formId , resultDiv , destinationUrl);
				    	    }
				      },
				      error:function(data){
				          alert("failure");
				         // $("#" + resultDiv).html('there is error while submit');
				      }   
				    }); 
			});

	}


	function loadOptionSelect(selectId , destinationURL){
		var select= document.getElementById(selectId);
		 select.options[0]= new Option("--Select--", "");
		 $.ajax({
		      url: destinationURL,
		      type: "post",
		      success: function(data){
			     // alert(data.length);
			      for(var i = 0; i < data.length ; i++){
			    	  select.options[i + 1]= new Option(data[i].label, data[i].value);
			      }
			      if(data.length > 0)
			    	  select.options[1].selected = true;
		      },
		      error:function(data){
		          alert("failure");
		      }   
		    }); 
	}

	function cancelAction(el , page , dividentifier){
		$("#div_" + page + "_" + dividentifier).slideUp("slow");
			//(){
			//		$("#div_" + page + "_" + dividentifier).html("");
			//});
		
		//alert("cancel action");
	}

	function deleteRecord(url ,containerid , page , id){
		confirmDelete = confirm("Are you sure that You want to delete this record?");

		if(confirmDelete){
			 $.ajax({
			      url: url,
			      type: "post",
			      success: function(data){
				    //  alert(data.success);
			      },
			      error:function(data){
			          alert("failure");
			      }   
			    }); 
			
			$("#" + containerid).fadeOut("slow");
		}
	}
</script>